<template>
  <div class="GRiskRankList">
    <filterPanel filterName="GRiskRankListFilter" filterTitle="风险评估排行查询" :filterList="filterData"
                 @filterChange="filterChange"></filterPanel>
    <tablePanel tableName="GRiskRankListTable" :tableLoading="loading" tableTitle="风险评估排行列表" :tableList="tableList"
                :tableData="tableData" :total="total" :currentPage="form.page_start" :pageSize="form.page_size"
                @sizeChange="sizeChange" @pageChange="pageChange" @tableClick="tableClick">
      <zy-button @click="add" type="primary">新增排行</zy-button>
      <zy-button @click="exportExcel">导出列表</zy-button>
    </tablePanel>
  </div>
</template>

<script>
	import {mapGetters} from "vuex";

	export default {
		name: "GRiskRankList",
		props: {},
		data() {
			return {
				filterData: [
					{
						title: '排行名称',
						name: 'risk_assmessment_name',
						type: 'input',
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '统计对象',
						name: 'risk_assmessment_obj',
						type: 'select',
						optionList: [
							{label: '全部', value: ''},
							{label: '物流企业', value: '1'},
							{label: '合作站点', value: '2'},
							{label: '从业人员', value: '3'},
							{label: '监管辖区', value: '4'},
							{label: '高危人员', value: '5'},
							{label: '高危物品', value: '6'},
							{label: '高危线路', value: '7'},
							{label: '高危电话', value: '8'},
							{label: '高危行为', value: '9'},
						],
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '统计人',
						name: 'create_name',
						type: 'input',
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '统计时间',
						name: 'time',
						type: 'date',
						value: [],
						isShow: true,
						isSelected: true
					},
					{
						title: '生成时间',
						name: 'ctime',
						type: 'date',
						value: [],
						isShow: true,
						isSelected: true
					},
				],
				form: {
					risk_assmessment_name: '',
					risk_assmessment_obj: '',
					create_name: "",
					time: [],
					ctime: [],
					page_start: 1,
					page_size: 10
				},
				loading: false,
				tableList: [
					{
						title: '排行名称',
						width: 120,
						name: 'risk_assmessment_name',
						isShow: true,
						isSelected: true
					},
					{
						title: '统计对象',
						width: 120,
						name: 'risk_assmessment_obj_str',
						isShow: true,
						isSelected: true
					},
					{
						title: '统计人',
						width: 120,
						name: 'create_name',
						isShow: true,
						isSelected: true
					},
					{
						title: '统计时间',
						width: 200,
						name: 'out_times',
						isShow: true,
						isSelected: true
					},
					{
						title: '生成时间',
						width: 200,
						name: 'create_time_str',
						isShow: true,
						isSelected: true
					},
					{
						title: '操作',
						width: 200,
						name: 'handler',
						fixed: 'right',
						btns: [
							{
								type: 'detail',
								name: '详情'
							},
							{
								type: 'download',
								name: '下载'
							},
							{
								type: 'put',
								name: '推送消息'
							},
							{
								type: 'delete',
								name: '删除',
								btnType: 'danger'
							},
						],
						isSelected: true
					}
				],
				tableData: [],
				total: 0,
				download_url: ''
			};
		},
		computed: {
			postData() {
				let data = {
					risk_assmessment_name: this.form.risk_assmessment_name,
					risk_assmessment_obj: this.form.risk_assmessment_obj,
					create_name: this.form.create_name,
					pagination: {
						page_start: this.form.page_start,
						page_size: this.form.page_size
					}
				};
				if (this.form.time && this.form.time.length > 0) {
					data.start_time = this.form.time[0] / 1000;
					data.end_time = this.form.time[1] / 1000;
				}
				if (this.form.ctime && this.form.ctime.length > 0) {
					data.l_post_date_start = this.form.ctime[0] / 1000;
					data.l_post_date_end = this.form.ctime[1] / 1000;
				}
				return data;
			},
			...mapGetters(['authority']),
			isAdd() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].i;
			},
			isLook() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].i;
			},
			isPut() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].p;
			},
			isDownload() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].d;
			},
			isDel() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].d;
			},
		},
		watch: {
			$route: {
				handler() {
					let {isRefresh} = this.$route.query || {};
					if (isRefresh) this.getData();
				},
				deep: true
			}
		},
		components: {},
		methods: {
			//导出
			exportExcel() {
				this.$import_excal(this.download_url || "");
			},
			// 新增
			add() {
				this.$toRoutePath({
					path: "/risk_rank_list/risk_rank_detail",
					edit: true,
					query: {type: 'add'}
				});
			},
			sizeChange(val) {
				this.form.page_size = val;
				this.getData()
			},
			pageChange(val) {
				this.form.page_start = val;
				this.getData()
			},
			filterChange(data) {
				this.form = {...this.form, ...data};
				this.getData();
			},
			getData() {
				this.loading = true;
				this.tableData = [];
				this.$axios.post(this.$api.g_getRiskAssmessment, {...this.postData}).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						this.tableData = data.list.filter(item => {
							item.detail = this.isLook || true;
							item.download = this.isDownload || true;
							item.put = this.isPut || true;
							item.delete = this.isDel || true;
							return item;
						});
						this.total = data.pagination.total_count || 0;
						this.download_url = data.download_url || ''
					} else {
						this.total = 0;
						this.tableData = []
					}
				}).catch(ret => {
					this.loading = false;
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			tableClick(data, type) {
				let arr = [], time = [];
				switch (type) {
					case 'detail':
						arr = data.out_times.split('~');
						console.log(arr)
						time = [parseInt(new Date(arr[0]).getTime()), parseInt(new Date(arr[1]).getTime())]
						this.toDetail(data.risk_assmessment_name, data.risk_assmessment_obj, time, type)
						break;
					case 'put':
						this.toPut(data.risk_assmessment_name);
						break;
					case 'download':
						break;
					case 'delete':
						this.deleted(data.risk_assmessment_id)
						break;
				}
			},
			toDetail(risk_assmessment_name, risk_assmessment_obj, time, type) {
				this.$toRoutePath({
					path: "/risk_rank_list/risk_rank_detail",
					edit: true,
					query: {risk_assmessment_name, risk_assmessment_obj, time, type}
				});
			},
			toPut(risk_assmessment_name) {
				this.$toRoutePath({
					path: "/notification_list/notification_detail",
					edit: true,
					query: {risk_assmessment_name, type: 'put'}
				});
			},
			// 删除
			deleted(risk_assmessment_id) {
				this.$confirm("此操作将永久删除该排名, 是否继续?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
					customClass: 'message',
				}).then(() => {
					this.$axios.post(this.$api.g_delRiskAssmessment, {risk_assmessment_id}).then(res => {
						let {code, msg} = res;
						if (code === 200) {
							this.$message.success("删除成功!");
							this.getData();
						} else {
							this.$message.error(msg);
						}
					})
				}).catch(() => {
					this.$message.info("已取消删除");
				});
			},
		},
		mounted() {
			this.getData()
		},
		created() {
		}
	}
</script>

<style scoped lang="less" src="@/style/government/riskAssessmentManage/rank/index.less">

</style>
